package com.shgsh.platform.service;

import com.alibaba.fastjson.JSON;
import com.shgsh.attribute.model.ActionData;
import com.shgsh.common.repository.CURDRepository;
import com.shgsh.common.util.DataTable;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by jessy on 2015/8/8.
 */
@Service
public class ActionDataService {

    Logger log = LoggerFactory.getLogger(ActionDataService.class);
    @Autowired
    CURDRepository curdRepository;



    /**
     * 保存行为数据
     * @param actionData
     */
    public void save(ActionData actionData){
        if(actionData != null){
            actionData.setAction_id(UUID.randomUUID().toString());
            log.debug("准备保存行为数据 " + actionData.getAction_id());
            String jsonData = actionData.toJson();
            System.out.println("json= " + jsonData);
            ArrayList<byte[]> hsetParams = new ArrayList<>();
            hsetParams.add(DataTable.ACTION_TABLE.getBytes());
            hsetParams.add(actionData.getAction_id().getBytes());
            hsetParams.add(jsonData.getBytes());
            curdRepository.hset(hsetParams);
            log.debug("成功保存行为数据 " + actionData.getAction_id());
        }
    }

    /**
     * 根据行为ID 获取行为数据
     * @param actionID
     * @return
     */
    public ActionData get(String actionID){
        if(!StringUtils.isEmpty(actionID)){
            log.debug("获取 行为ID为 " + actionID + " 的行为数据 .");
            ArrayList<byte[]> hgetParams = new ArrayList<>();
            hgetParams.add(DataTable.ACTION_TABLE.getBytes());
            hgetParams.add(actionID.getBytes());
            String result = curdRepository.hget(hgetParams);
            ActionData ad = JSON.parseObject(result, ActionData.class);
            return ad;
        }
        return null;
    }

    /**
     * 删除指定行为ID的行为数据
     * @param actionID
     */
    public void delete(String actionID){
        if(!StringUtils.isEmpty(actionID)){
            ArrayList<byte[]> hdelParams = new ArrayList<>();
            hdelParams.add(DataTable.ACTION_TABLE.getBytes());
            hdelParams.add(actionID.getBytes());
            curdRepository.hdel(hdelParams);
            log.debug("删除 行为ID为 " + actionID + " 的行为数据");
        }
    }

    /**
     * 更新行为数据
     * @param actionData
     */
    public void update(ActionData actionData){
        ActionData ad = get(actionData.getAction_id());
        ad.setEffect(actionData.getEffect());
        ad.setSection(actionData.getSection());
        ad.setDescription(actionData.getDescription());
        ad.setRequire(actionData.getRequire());
        ad.setFeature(actionData.getFeature());
        save(ad);
        log.debug("更新 id为 " + actionData.getAction_id() + " 的行为数据");
    }

    /**
     * 列出hashmap 指定表名的所有数据
     * @return
     */
    public List<ActionData> list(){
        Map<String, String> map = curdRepository.hscanAll(DataTable.ACTION_TABLE);
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        ArrayList<ActionData> actionDatas = new ArrayList<>();
        for(Map.Entry<String, String> it : entrySet){
            String actionData = it.getValue();
            actionDatas.add(JSON.parseObject(actionData, ActionData.class));
        }
        log.debug("获取表 " + DataTable.ACTION_TABLE + " 的所有数据。");
        return actionDatas;
    }
    public List<String> listAttributes(){
        Map<String, String> map = curdRepository.hscanAll(DataTable.ATTR_INITIAL_DATA);
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        ArrayList<String> datas = new ArrayList<>();
        for(Map.Entry<String, String> it : entrySet){
            String data = it.getKey();
            datas.add(data);
        }
        System.out.println("datas = " + datas.size());
        log.debug("获取表 " + DataTable.ATTR_INITIAL_DATA + " 的所有key值。");
        return datas;
    }

}
